<?php


namespace app\api\controller\xilujob;


use app\common\controller\xilujob\XilujobApi;
use app\common\model\xilujob\Sentence AS SentenceModel;
use think\Db;

class Sentence extends XilujobApi
{

    protected $noNeedRight = ['*'];
    protected $restrictedActions = ['*'];


    /**
     * 常用语
     */
    public function index(){
        $lists = SentenceModel::where("user_id",0)
            ->whereOr('user_id',$this->auth->id)
            ->order('id','desc')
            ->select();
        $this->success('成功',$lists);
    }

    /**
     * 常用语
     */
    public function add_sentence(){
        $content = $this->request->param('content');
        if(!$content){
            $this->error('常用语不得为空');
        }
        $sentence_id = $this->request->param('sentence_id','');
        if(!$sentence_id){
            $ret = SentenceModel::create([
                'user_id'   =>  $this->auth->id,
                'name'      =>  $content,
            ]);
        }else{
            $ret = SentenceModel::where('id',$sentence_id)->update([
                'name'      =>  $content,
            ]);
        }
        $this->success('成功',$ret);
    }

    public function del_sentence(){
        $sentence_id = $this->request->param('sentence_id','');
        $sentence = $sentence_id?SentenceModel::get($sentence_id):null;
        if(!$sentence){
            $this->error('no record');
        }
        if($sentence->user_id == 0){
            $this->error("系统语不可删除");
        }
        $sentence->delete();

        $this->success('删除成功');
    }


}